<?php
	$dbnameID="";
	$dbQueryCount=0;
	$dbversion="";

// mySQL database functions
function dbquery($query) {
	global $dbQueryCount;
	$dbQueryCount++;
	$result = @mysql_query($query);
	if (!$result) {
		echo mysql_error();
		return false;
	} else {
		return $result;
	}
}

function stripslash($text) {
	if (QUOTES_GPC) $text = stripslashes($text);
	return $text;
}

function quote_real_escape_string($value){
	if(!is_numeric($value)){
		$value=mysql_real_escape_string($value);
	}
	return $value;
}

function qre($value){
	return quote_real_escape_string($value);
}

function dbconnect($db_host, $db_user, $db_pass, $db_name) {
	global $dbnameID; global $dbversion;
	$dbnameID=$db_name;
	$db_connect = @mysql_connect($db_host, $db_user, $db_pass);
	$db_select = @mysql_select_db($db_name);

	$dbversion=mysql_get_server_info($db_connect);

	if (!$db_connect) {
		die("<div style='font-family:Verdana;font-size:11px;text-align:center;'><b>Unable to establish connection to MySQL</b><br>".mysql_errno()." : ".mysql_error()."</div>");
	} elseif (!$db_select) {
		die("<div style='font-family:Verdana;font-size:11px;text-align:center;'><b>Unable to select MySQL database</b><br>".mysql_errno()." : ".mysql_error()."</div>");
	}
}

function dbcount($field,$table,$conditions=""){
	global $dbQueryCount; $dbQueryCount++;
	$cond = ($conditions ? " WHERE ".$conditions : "");
	$result = @mysql_query("SELECT Count".$field." FROM ".$table.$cond);
	if (!$result) {
		echo mysql_error();
		return false;
	} else {
		$rows = mysql_result($result, 0);
		return $rows;
	}
}

function dbresult($query, $row) {
	$result = @mysql_result($query, $row);
	if (!$result) {
		echo mysql_error();
		return false;
	} else {
		return $result;
	}
}

function dbrows($query) {
	$result = @mysql_num_rows($query);
	return $result;
}

function dbarray($query) {
	$result = @mysql_fetch_assoc($query);
	if (!$result) {
		echo mysql_error();
		return false;
	} else {
		return $result;
	}
}

function dbarraynum($query) {
	$result = @mysql_fetch_row($query);
	if (!$result) {
		echo mysql_error();
		return false;
	} else {
		return $result;
	}
}

function sql_table_exists($tablename){
	global $dbnameID;
	global $dbQueryCount; $dbQueryCount++;
	$table_list = mysql_query("SHOW TABLES FROM `".$dbnameID."`");
	while ($row = mysql_fetch_row($table_list)) {
		if ($tablename==$row[0]) {
			return true;
		}
	}
	return false;
}

function sql_fetchrow($query_id = 0){
		if( !$query_id )
		{
			$query_id = 0;
		}
		if($query_id){
			$row = array();
			$row[$query_id] = mysql_fetch_array($query_id, MYSQL_ASSOC);
			return $row[$query_id];
		}
		else
		{
			return false;
		}
}

function sql_array($query_id = 0){
	$topic_rowset = array();
	if( !$query_id ){
		$query_id = 0;
	}

	while( $row = sql_fetchrow($query_id) ){
		$topic_rowset[] = $row;
	}
	return $topic_rowset;
}


function sql_select($table, $array_param, $array_value,$conditions=""){
	$cond = ($conditions ? " WHERE ".$conditions : "");
	$query="SELECT * FROM ".$table.$cond;

	echo $query;
}

function sql_update($table, $array_param, $array_value,$conditions=""){
	$cond = ($conditions ? " WHERE ".$conditions : "");
	$query="UPDATE `".$table."` SET ";

	for($i=0; $i<count($array_param); $i++){
		if($i==0){
			$query.=" ".$array_param[$i]."='".quote_real_escape_string($array_value[$i])."'";
		}else{
			$query.=", ".$array_param[$i]."='".quote_real_escape_string($array_value[$i])."'";
		}
	}
	$query.=$cond;

	return dbquery($query);
}

function sql_delete($table, $conditions=""){
	$cond = ($conditions ? " WHERE ".$conditions : "");
	$query="DELETE FROM `".$table."` ".$cond;

	echo $query;
}

function sql_insert($table, $array_param, $array_value){

	$query="INSERT INTO `".$table."`";

	$query.=" (";
	for($i=0; $i<count($array_param); $i++){
		if($i==0){
			$query.="".$array_param[$i];
		}else{
			$query.=", ".$array_param[$i];
		}
	}
	$query.=")";

	$query.=" VALUES(";
	for($i=0; $i<count($array_param); $i++){
		if($i==0){
			$query.="'".quote_real_escape_string($array_value[$i])."'";
		}else{
			$query.=", '".quote_real_escape_string($array_value[$i])."'";
		}
	}
	$query.=")";

	return dbquery($query);
}



function normaliz($a) {
	return preg_replace('/[^a-zA-Z0-9_\.-]/', '', $a);
}

function isNum($value) {
	return (preg_match("/^[0-9]+$/", $value));
}

function normaliz2num($a) {
	return preg_replace('/[^0-9]/', '', $a);
}

function showdate($format, $val) {
        global $SETTINGS;
        global $locale;

        $d1=date("d", $val);
        $m1=date("m", $val);
        $y1=date("Y", $val);
        $dpub=mktime(0,0,0,$m1,$d1,$y1);

        $d2=date("d");
        $m2=date("m");
        $y2=date("Y");
        $dtop=mktime(0,0,0,$m2,$d2,$y2);

        $dti=$dtop-$dpub;
        $d3=floor($dti /(24*60*60));

        if($d3<1 && $SETTINGS['use_showdate_ext']==1){
                return $locale['now']." ".strftime($SETTINGS['dt_format_timedate'], $val);

        }elseif($d3==1 && $SETTINGS['use_showdate_ext']==1){
                return $locale['vchera']." ".strftime($SETTINGS['dt_format_timedate'], $val);
        }else{
                return strftime($format, $val);
        }
}

function breaker($string){

	$string=str_replace("\r", "", $string);
	$string=str_replace("\n", "<br>\r\n", $string);
	//$string=str_replace("\t", "", $string);

	return $string;
}


function anti_script($string){

	$string=htmlspecialchars($string);
	$string=str_replace("'", "&#39;", $string);
	$string=str_replace("\"", "&#34;", $string);
	$string=breaker($string);

	return $string;
}

function prepare_global_name($text) {
	$text = stripslash($text);
	$search = array("\"", "'", "\\", '\"', "\'", "&nbsp;");
	$replace = array("", "", "", "", "", " ");
	$text = str_replace($search, $replace, $text);
	return $text;
}

function stripinput($text){
	$search = array("\"", "'", "\\", '\"', "\'", "<", ">", "&nbsp;");
	$replace = array("&quot;", "&#39;", "&#92;", "&quot;", "&#39;", "&lt;", "&gt;", " ");
	$text = str_replace($search, $replace, $text);
	return $text;
}

?>